package com.lc.cehui.widget;

import android.app.Dialog;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.imageview.ShapeableImageView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.base.BaseActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.CarInsertApi;
import com.lc.cehui.http.request.GoodsXQ;
import com.lc.cehui.http.response.GoodsDetailsBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.OrderDetailsActivity;
import com.lc.cehui.util.SPUtil;

public class ShoppingDialog {

    public AppActivity activity;
    public Dialog dialog;
    private Button bt_car, tv_submit;
    private GoodsDetailsBean.DataBean dataBean;
    private TextView tv_money,tv_goods_id;
    private AmountView amountView;
    private Adapter adapter;

    public ShoppingDialog(AppActivity activity) {
        this.activity = activity;
    }

    public void Buynow(String json) {
        dataBean = JSONObject.parseObject(json, GoodsDetailsBean.DataBean.class);
        LogUtils.e(JSON.toJSONString(dataBean));
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_goods_info, null, false);
        dialog = new Dialog(activity, R.style.DialogCentre);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        initView(view);
        ShapeableImageView iv_goods_logo = view.findViewById(R.id.iv_goods_logo);
        tv_money = view.findViewById(R.id.tv_money);
        tv_goods_id = view.findViewById(R.id.tv_goods_id);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        amountView = view.findViewById(R.id.amountView);
        GlideApp.with(activity).load(dataBean.getCover_img()).into(iv_goods_logo);
        tv_money.setText("¥"+dataBean.getPrice());

        for (int i = 0; i < dataBean.getAttr().size(); i++) {
            if (dataBean.getAttr().get(i).getPrice().equals(dataBean.getPrice())){
                dataBean.getAttr().get(i).setSelect(true);
                amountView.setGoods_storage(Integer.valueOf(dataBean.getAttr().get(i).getStock()));
                break;
            }
        }
        
        recyclerView.setAdapter(adapter = new Adapter(R.layout.adapter_goods_model));
        adapter.setNewInstance(dataBean.getAttr());
        dialog.show();
    }

    private void initView(View view) {
        bt_car = view.findViewById(R.id.bt_car);
        tv_submit = view.findViewById(R.id.tv_submit);
        bt_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyHttp.post(activity)
                        .api(new CarInsertApi().setUserid(SPUtil.get(activity, IntentKey.USERID, 0))
                                .setNum(amountView.getAmount()).setGoods_id(dataBean.getId()).setStore_id(dataBean.getStore_id()))
                        .request(new HttpCallback<GoodsDetailsBean>(activity) {
                            @Override
                            public void onSucceed(GoodsDetailsBean data) {
                                LogUtils.e(JSON.toJSONString(data));
                                ToastUtils.showShort("加入成功");
                                dialog.dismiss();
                            }
                        });
            }
        });
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, OrderDetailsActivity.class);
                activity.startActivity(intent);
                dialog.dismiss();
            }
        });
    }

    class Adapter extends BaseQuickAdapter<GoodsDetailsBean.DataBean.AttrBean,BaseViewHolder> {

        public Adapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, GoodsDetailsBean.DataBean.AttrBean attrBean) {
            holder.setText(R.id.tv_attrs,attrBean.getName());
            if (attrBean.isSelect()){
                holder.setTextColor(R.id.tv_attrs,activity.getResources().getColor(R.color.blue168));
            } else {
                holder.setTextColor(R.id.tv_attrs,activity.getResources().getColor(R.color.grayc1c1));
            }
            holder.getView(R.id.tv_attrs).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0; i < dataBean.getAttr().size(); i++) {
                        dataBean.getAttr().get(i).setSelect(false);
                    }
                    attrBean.setSelect(true);
                    adapter.notifyDataSetChanged();
                    tv_money.setText("¥"+attrBean.getPrice());
                    amountView.setGoods_storage(Integer.valueOf(attrBean.getStock()));
                }
            });

        }
    }

}
